package org.openbpm.bus.model.xml;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import lombok.Data;
import org.openbpm.bus.model.BusColumnCtrl;
import org.openbpm.bus.model.BusinessColumn;
import org.openbpm.bus.model.BusinessTable;

import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@XmlRootElement(name = "busTable")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
public class BusTableXml {

    @XmlElements({@XmlElement(name="busColumn", type= BusColumnXml.class)})
    private List<BusColumnXml> busColumnList = new ArrayList<>();

    @XmlAttribute
    private String key;

    @XmlAttribute
    private String name;

    @XmlAttribute
    private String dsKey;

    @XmlAttribute
    private String dsName;
    @XmlAttribute
    private String groupId;
    @XmlAttribute
    private String groupName;
    @XmlAttribute
    private boolean external;
    @XmlAttribute
    private String comment;

    public BusTableXml() {
    }

    public BusTableXml(BusinessTable filledTable) {
        BeanUtil.copyProperties(filledTable, this);
        List<BusinessColumn> columnList = filledTable.getColumns();
        if(columnList==null){
            return;
        }
        for(BusinessColumn column: columnList){
            BusColumnXml columnXml = new BusColumnXml();
            BeanUtil.copyProperties(column, columnXml);
            BusColumnCtrl columnCtrl = column.getCtrl();
            if(columnCtrl!=null){
                columnCtrl.setId(null);
                columnCtrl.setColumnId(null);
                columnCtrl.setCreateTime(null);
                columnCtrl.setCreateBy(null);
                columnCtrl.setUpdateTime(null);
                columnCtrl.setUpdateBy(null);
                columnXml.setCtrJson(JSON.toJSONString(columnCtrl));
            }
            busColumnList.add(columnXml);
        }
    }

}
